package com.example.up.view;

import java.text.NumberFormat;

import com.example.up.R;
import com.example.up.util.HttpUtil;
import com.example.up.util.UpdateManager.DownloadTask;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class CommonProgressDialog extends AlertDialog {

	DownloadTask task;
	Context context1;

	public CommonProgressDialog(Context context, DownloadTask task) {
		super(context, R.style.Dialog_Fullscreen);
		setOwnerActivity((Activity) context);
		this.context1 = context;
		this.task = task;
		initFormats();

		Window dialogWindow = this.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);
		// 显示的坐标
		lp.x = 0;
		lp.y = 1;
		dialogWindow.setAttributes(lp);

	}

	private ProgressBar mProgress;
	private TextView mProgressNumber;
	private TextView mProgressPercent;
	private TextView mProgressMessage;
	private int mMax;
	private String mProgressNumberFormat;
	private NumberFormat mProgressPercentFormat;
	private Button cancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_moco);
		this.getContext();
		mProgress = (ProgressBar) findViewById(R.id.progress);
		mProgressNumber = (TextView) findViewById(R.id.progress_number);
		mProgressPercent = (TextView) findViewById(R.id.progress_percent);
		mProgressMessage = (TextView) findViewById(R.id.progress_message);
		cancel = (Button) findViewById(R.id.confirm);

		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				if (task != null)
					task.exit();
				HttpUtil.start(context1);
			}
		});

	}

	private void initFormats() {
		mProgressNumberFormat = "%1.2fM/%2.2fM";
		mProgressPercentFormat = NumberFormat.getPercentInstance();
		mProgressPercentFormat.setMaximumFractionDigits(0);
	}

	public void setProgressStyle(int style) {
		// mProgressStyle = style;
	}

	public int getMax() {
		if (mProgress != null) {
			return mProgress.getMax();
		}
		return mMax;
	}

	public void setMax(int max) {
		if (mProgress != null) {
			mProgress.setMax(max);

		} else {
			mMax = max;
		}
	}

	public void setProgress(int value) {
		mProgress.setProgress(value);
	}

	public void setTextPercent(float percent) {
		if (mProgressPercentFormat != null) {
			SpannableString tmp = new SpannableString(
					mProgressPercentFormat.format(percent));
			tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
					tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			mProgressPercent.setText(tmp);
		} else {
			mProgressPercent.setText("");
		}
	}

	public int getProgress() {
		return mProgress.getProgress();
	}

	public void setTextNumber(int progress, int max) {

		double dProgress = (double) progress / (double) (1024 * 1024);
		double dMax = (double) max / (double) (1024 * 1024);
		if (mProgressNumberFormat != null) {
			String format = mProgressNumberFormat;
			mProgressNumber.setText(String.format(format, dProgress, dMax));
		} else {
			mProgressNumber.setText("");
		}
	}

	public void setTitleText(String text) {
		if (mProgressMessage != null)
			mProgressMessage.setText(text);
	}

}